package com.myinternet.lutu.service.impl;

import com.github.pagehelper.PageHelper;
import com.myinternet.lutu.mapper.AccessListMapper;
import com.myinternet.lutu.pojo.AccessList;
import com.myinternet.lutu.service.AccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Service 映射类的类型
 */
@Service
public class AccessServiceImpl implements AccessService {

    @Autowired
    private AccessListMapper accMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚
    public void saveAccess(AccessList accessList) {
        accMapper.insert(accessList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateAccess(AccessList accessList) {
        accMapper.updateByPrimaryKey(accessList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteAccess(Integer accessId) {
        accMapper.deleteByPrimaryKey(accessId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public AccessList queryAccessById(Integer accessId) {
        return accMapper.selectByPrimaryKey(accessId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<AccessList> queryAllAccess() {
        return accMapper.selectAll();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<AccessList> queryAccessPaged(AccessList accessList, Integer page, Integer pageSize) {
        //开始分页
        PageHelper.startPage(page, pageSize);

        Example example = new Example(AccessList.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmptyOrWhitespace(accessList.getAccessIp())) {
            criteria.andLike("nickname", "%" + accessList.getAccessIp() + "%");
        }
        example.orderBy("accessIp").desc();
        List<AccessList> access = accMapper.selectByExample(example);
        return access;
    }
}
